<template>
    <div class="container-fluid" v-loading="loading">

        <div class="row">
            <div class="col-lg-12">
                <div class="card">
                    <div class="card-body">
                        <div>
                            <form class="form-inline" onsubmit="return false;">


                                <div class="row" style="margin-top:15px;">

                                    <div class="form-group">
                                        <!--<label class="el-form-item__label" style="width: 120px;">关联分类</label>-->
                                        <el-autocomplete
                                                v-model="companyName"
                                                :fetch-suggestions="queryCompanyAsync"
                                                placeholder="公司/品牌"
                                                @select="handleSearchSelect"
                                                style="width: 100%"
                                        >
                                        </el-autocomplete>
                                    </div>
                                    <div class="form-group">
                                        <el-tag
                                                :key="tag.id"
                                                v-for="tag in tags"
                                                closable
                                                :disable-transitions="false"
                                                @close="handleSearchClose(tag)">
                                            {{tag.value}}
                                        </el-tag>
                                    </div>


                                    <div class="form-group">
                                        <!--<label class="el-form-item__label" style="width: 120px;">关联分类</label>-->
                                        <el-autocomplete
                                                v-model="categoryName"
                                                :fetch-suggestions="querySearchAsync"
                                                placeholder="分类/分类"
                                                @select="handleIndSearchSelect"
                                                style="width: 100%"
                                        >
                                        </el-autocomplete>
                                    </div>
                                    <div class="form-group">
                                        <el-tag
                                                :key="tag.id"
                                                v-for="tag in indTags"
                                                closable
                                                :disable-transitions="false"
                                                @close="handleIndSearchClose(tag)">
                                            {{tag.value}}
                                        </el-tag>
                                    </div>

                                    <div class="form-group">
                                        <label class="sr-only" for="example-if-password">请文章标题</label>
                                        <input class="form-control" type="text" id="example-if-password"
                                               name="example-if-password" placeholder="请文章标题" v-model="keyword" autocomplete="off">
                                    </div>


                                    <div class="form-group">
                                        <button class="btn btn-success" type="submit" v-on:click="searchClick()">筛选</button>
                                    </div>
                                </div>


                            </form>
                        </div>


                        <div class="table-responsive" style="width: 100%;margin-top: 20px;" v-loading="loading">
                            <el-table
                                    :data="list"
                                    style="width: 100%"
                                    @sort-change='sortChange'
                                    :default-sort="{prop: '_id', order: 'descending'}"
                            >
                                <el-table-column
                                        label="ID"
                                        type="index"
                                        :index="indexMethod"
                                        width="40">
                                </el-table-column>
                                <el-table-column
                                        prop="title"
                                        label="文章标题"
                                        align="center"
                                        width="160">
                                    <template slot-scope="scope">
                                        <a :href="'/admin/article/'+scope.row._id+'/edit'" target="_blank" style="cursor:pointer;">{{scope.row.title
                                            }}</a>
                                    </template>
                                </el-table-column>
                                <el-table-column
                                        prop="userName"
                                        label="上传用户"
                                        align="center"
                                        width="80">
                                </el-table-column>
                                <el-table-column
                                        prop="companyStr"
                                        label="所属公司"
                                        align="center"
                                        width="100">
                                </el-table-column>
                                <el-table-column
                                        prop="categoryStr"
                                        label="所属分类"
                                        align="center"
                                        width="70">
                                </el-table-column>
                                <el-table-column
                                        prop="fromAccount"
                                        label="来源公众号"
                                        align="center"
                                        width="120">
                                </el-table-column>
                                <el-table-column
                                        prop="created_at"
                                        label="上传时间"
                                        align="center"
                                        sortable
                                        width="170">
                                </el-table-column>
                                <el-table-column
                                        label="操作"
                                        align="center"
                                >
                                    <template slot-scope="scope">
                                        <span class="span-success" @click="handleEdit(scope.row,1)">通过</span>
                                        <span class="span-success" @click="handleEdit(scope.row,-1)">不通过</span>
                                        <span class="span-danger" @click="handleDelete(scope.row)">删除</span>
                                        <a v-if="scope.row.originalUrl" :href="scope.row.originalUrl"
                                           target="_bland">原文</a>
                                    </template>

                                </el-table-column>
                            </el-table>

                            <nav style="margin-top: 20px">
                                <el-pagination
                                        layout="total, sizes, prev, pager, next, jumper"
                                        :total="total"
                                        :hide-on-single-page="pageActive"
                                        @size-change="handleSizeChange"
                                        :page-size="pageSize"
                                        :current-page="currentPage"
                                        :page-sizes="[10, 20, 30, 40]"
                                        @current-change="handleCurrentChange"
                                >
                                </el-pagination>

                            </nav>

                            <el-dialog title="文章审核" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
                                <el-form :model="form">

                                    <el-form-item label="" :label-width="formLabelWidth">
                                        <h5>{{articleTitle}}</h5>
                                    </el-form-item>


                                    <el-form-item label="关联分类" :label-width="formLabelWidth">
                                        <el-autocomplete
                                                v-model="form.categoryName"
                                                :fetch-suggestions="querySearchAsync"
                                                placeholder="请输入内容"
                                                @select="handleQuerySelect"
                                                style="width: 100%"
                                        ></el-autocomplete>
                                        <el-tag
                                                :key="tag.id"
                                                v-for="tag in categoryTags"
                                                closable
                                                :disable-transitions="false"
                                                @close="handleIndustryClose(tag)">
                                            {{tag.value}}
                                        </el-tag>
                                    </el-form-item>

                                    <el-form-item label="关联公司" :label-width="formLabelWidth">
                                        <el-autocomplete
                                                v-model="form.companyName"
                                                :fetch-suggestions="queryCompanyAsync"
                                                placeholder="请输入内容"
                                                @select="handleCompanySelect"
                                                style="width: 100%"
                                        ></el-autocomplete>
                                        <el-tag
                                                :key="tag.id"
                                                v-for="tag in companyTags"
                                                closable
                                                :disable-transitions="false"
                                                @close="handleCompanyClose(tag)">
                                            {{tag.value}}
                                        </el-tag>
                                    </el-form-item>
                                </el-form>
                                <div slot="footer" class="dialog-footer">
                                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                                    <el-button type="primary" @click="submitSet">{{ verifyName }}</el-button>
                                </div>
                            </el-dialog>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    import common from '../../api/public.js';
    export default {
        name: "ArticleVerifyComponent",
        data() {
            return {
                pageActive: false,
                list: [],
                pageSize: 10,
                total: 0,
                currentPage: 1,
                loading: true,
                activeIndex: 1,
                keyword:'',
                form: {
                    name: '',
                    categoryName: '',
                    companyName: ''
                },
                categoryTags: [],
                categoryIds: [],
                companyTags: [],
                companyIds: [],
                dialogFormVisible: false,
                formLabelWidth: '120px',
                companies: [],
                restaurants: [],
                id: 0,
                order: 'desc',
                orderField: '_id',
                articleTitle: '',
                verifyName: '确 定',
                verifyStatus: 1,
                tags: [],
                ids: [],
                companyName:'',
                //分类搜索
                categoryName:'',
                indTags: [],
                indIds: [],
            }
        },
        methods: {
            ...common,
            search: function () {
                let self = this;
                let uri = 'article/video/cartoon/post-list';
                self.loading = true;
                let postData = {
                    type:1,
                    startTime: self.startTime,
                    endTime: self.endTime,
                    keyword: self.keyword,
                    page: self.currentPage,
                    limit: self.pageSize,
                    status: self.status,
                    order: self.order,
                    orderField: self.orderField,
                    verifyStatus: 0,
                    companyIds: self.ids,
                    categoryIds:self.indIds
                };
                axios.post(uri, postData).then(function (result) {
                    self.total = result.data.total;
                    if (self.total > 0) {
                        self.pageActive = false;
                    } else {
                        self.pageActive = true;
                    }
                    self.list = result.data.data;
                    self.loading = false;
                }).catch(function (error) {
                    self.loading = false;
                });
            },
            handleSearchClose(tag) {
                this.tags.splice(this.tags.indexOf(tag), 1);
                this.ids.splice(this.ids.indexOf(tag.id), 1);
            },
            handleIndSearchSelect(item) {
                if (item && this.ids.indexOf(item.id) === -1) {
                    this.indTags.push(item);
                    this.indIds.push(item.id);
                }
                this.categoryName = '';
            },
            handleIndSearchClose(tag) {
                this.indTags.splice(this.indTags.indexOf(tag), 1);
                this.indIds.splice(this.indIds.indexOf(tag.id), 1);
            },

            handleSearchSelect(item) {
                if (item && this.ids.indexOf(item.id) === -1) {
                    this.tags.push(item);
                    this.ids.push(item.id);
                }
                this.companyName = '';
            },
            handleEdit(row, verify) {
                let self = this;

                self.verifyStatus = verify;

                if (verify === 1) {
                    self.verifyName = "保存并通过审核";
                } else {
                    self.verifyName = "保存并不通过审核";
                }
                self.dialogFormVisible = true;
                self.form.categoryName = '';
                self.form.companyName = '';
                self.companyIds = [];
                self.companyTags = [];
                self.categoryIds = [];
                self.categoryTags = [];
                self.articleTitle = row.title;
                self.categoryTags = row.categoryNames;

                row.categoryNames.forEach((item) => {
                    self.categoryIds.push(item.id)
                });
                self.companyTags = row.companies;
                row.companies.forEach((item) => {
                    self.companyIds.push(item.id)
                });
                self.id = row._id;
            },
            openEdit(id) {
                this.openUri('article/' + id + '/edit');
            },
            handleDelete: function (row) {
                let self = this;
                let tipStr = "此操作将永久删除“" + row.title + "”, 是否继续?";
                self.$confirm(tipStr, '文章删除', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    self.deleteRequireArticleVideoCartoon(row._id)
                }).catch(() => {
                    self.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            submitSet() {
                let self = this;
                let categoryIds = this.categoryIds;
                let companyIds = this.companyIds;

                /*if (categoryIds.length > 0 && companyIds.length > 0) {
                    self.$message.error('不能同时选择分类和公司！');
                    return;
                }*/
                self.loading = true;
                let uri = "article/set/" + self.id;
                let data = {companyIds: companyIds, categoryIds: categoryIds, verifyStatus: self.verifyStatus};

                if (self.verifyStatus == 1){
                    data.status = 1;
                }
                self.dialogFormVisible = false;
                axios.put(uri, data).then(function (result) {
                    self.search();
                }).catch(function () {
                    self.loading = false;
                });
            },
        },
        mounted() {
            this.search();
            this.queryIndustry();
            this.queryCompany();
        },
        filters: {
            titleFormatter(title) {
                if (title.length >= 10) {
                    return title.substring(0, 10) + '...';
                }
                return title.length === 0 ? '--' : title;
            }
        }
    }
</script>

<style scoped>

</style>
